/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Control.UTI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.*;

/**
 *
 * @author Augusto Wolf
 */
public class jpDisposicaoLab extends javax.swing.JPanel {

    /**
     * Creates new form jpDisposicaoLab
     */
    boolean[][] escolha = new boolean[10][10];

    public jpDisposicaoLab() {
        initComponents();

        jTable1.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected, boolean hasFocus, int r, int c) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, r, c);
                if (escolha[r][c]) {
                    setBackground(Color.YELLOW);
                } else {
                    setBackground(Color.WHITE);
                }
                return this;
            }
        });

        try {
            jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            jTable1.setTableHeader(null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();

        jLabel1.setText("Código do Laboratório:");

        jLabel2.setText("Linhas:");

        jTextField2.setText("0");
        jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                filtroClientes(evt);
            }
        });

        jTextField3.setText("0");
        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                filtroClientes(evt);
            }
        });

        jLabel3.setText("Colunas:");

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTable1.setFont(new java.awt.Font("Monospaced", 1, 13)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setSelectionBackground(null);
        jTable1.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.getTableHeader().setResizingAllowed(false);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTable1MouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Salvar");

        jLabel4.setText("Tamanho:");

        jTextField4.setText("20");
        jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                filtroClientes(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseReleased
        int r = jTable1.rowAtPoint(evt.getPoint());
        int c = jTable1.columnAtPoint(evt.getPoint());
        int rMax = jTable1.getRowCount();
        int cMax = jTable1.getColumnCount();

        if (r < 1) {
            r = 1;
        } else if (r > rMax - 2) {
            r = rMax - 2;
        }
        if (c < 1) {
            c = 1;
        } else if (c > cMax - 2) {
            c = cMax - 2;
        }

        boolean adiciona = true;
        for (int i = r - 1; i <= r + 1; i++) {
            for (int j = c - 1; j <= c + 1; j++) {
                try {
                    if (jTable1.getModel().getValueAt(i, j).toString().length() > 0) {
                        r = i;
                        c = j;
                        adiciona = false;
                    }
                } catch (Exception ex) {
                }
            }
        }
        if (adiciona) {


            for (int i = r - 2; i <= r + 2; i++) {
                for (int j = c - 2; j <= c + 2; j++) {
                    try {
                        if (jTable1.getModel().getValueAt(i, j).toString().length() > 0) {
                            adiciona = false;
                        }
                    } catch (Exception ex) {
                    }
                }
            }
            if (adiciona) {
                String s = UTI.lerString(this, "Informe o IP do computador.", "");
                if (s != null) {
                    for (int i = r - 1; i <= r + 1; i++) {
                        for (int j = c - 1; j <= c + 1; j++) {
                            escolha[i][j] = true;
                            jTable1.getModel().setValueAt("", i, j);
                        }
                    }
                    jTable1.getModel().setValueAt(s, r, c);
                }
            }
        } else {

            for (int i = r - 1; i <= r + 1; i++) {
                for (int j = c - 1; j <= c + 1; j++) {
                    escolha[i][j] = false;
                    jTable1.getModel().setValueAt("", i, j);
                }
            }

        }

    }//GEN-LAST:event_jTable1MouseReleased

    private void filtroClientes(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_filtroClientes
        filtroClientes();
    }//GEN-LAST:event_filtroClientes
    private long agora;
    private boolean rodando;

    private void filtroClientes() {
        agora = new Date().getTime();
        if (!rodando) {
            Thread t = new Thread() {

                public void run() {
                    rodando = true;
                    jProgressBar1.setIndeterminate(true);
                    while (agora + 1000 > new Date().getTime()) {
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                        }

                    }
                    int l = Integer.parseInt(UTI.soNumeros(jTextField2.getText()));
                    int c = Integer.parseInt(UTI.soNumeros(jTextField3.getText()));
                    int t = Integer.parseInt(UTI.soNumeros(jTextField4.getText()));
                    if (t < 20) {
                        t = 20;
                    }
                    jTextField2.setText(l + "");
                    jTextField3.setText(c + "");
                    jTextField4.setText(t + "");
                    alteraTabela(l, c, t);
                    jProgressBar1.setIndeterminate(false);
                    rodando = false;
                }
            };
            t.start();
        }
    }

    public void alteraTabela(int linhas, int colunas, int tam) {
        Font ff = new Font("Monospaced", Font.BOLD, (int) (tam / 1.5));
        jTable1.setFont(ff);
        boolean antes[][] = new boolean[escolha.length][escolha[0].length];
        for (int i = 0; i < antes.length; i++) {
            for (int j = 0; j < antes[0].length; j++) {
                antes[i][j] = escolha[i][j];
            }
        }
        escolha = new boolean[linhas][colunas];

        for (int i = 0; i < antes.length && i < linhas; i++) {
            for (int j = 0; j < antes[0].length && i < colunas; j++) {
                escolha[i][j] = antes[i][j];
            }
        }

        ((DefaultTableModel) jTable1.getModel()).setRowCount(linhas);
        ((DefaultTableModel) jTable1.getModel()).setColumnCount(colunas);
        jTable1.setRowHeight(tam);
        for (int i = 0; i < colunas; i++) {
            try {
                jTable1.getColumnModel().getColumn(i).setPreferredWidth(tam);
                jTable1.getColumnModel().getColumn(i).setMinWidth(tam);
                jTable1.getColumnModel().getColumn(i).setMaxWidth(tam);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables
}
